import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/8 15:00
 * @description 深度优先遍历
 */
public class Solution2 {
    public static void main(String[] args) {

    }
    int maxDep = -1;
    int total = 0;
    public int deepestLeavesSum(TreeNode root) {
        dfs(root, 0);
        return total;
    }
    public void dfs(TreeNode node, int dep) {
        if (node == null) {
            return;
        }
        if (dep > maxDep) {
            maxDep = dep;
            total = node.val;
        }else if (dep == maxDep) {
            total += node.val;
        }
        dfs(node.left, dep + 1);
        dfs(node.right, dep + 1);
    }
}
